﻿using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using JESAI.Core.Configuration;
using JESAI.Data.Filters;
using JESAI.Datas.Sqlsugar.Extensions;
using JESAI.Datas.Sqlsugar.Filters;
using JESAI.Datas.Sqlsugar.Options;
using JESAI.Datas.Sqlsugar.Repositories;
using JESAI.Datas.Sqlsugar.Uow;
using JESAI.DDD.Repositories;
using JESAI.Uow;

namespace JESAI.Datas.Sqlsugar
{
    public static class ServiceCollectionExtensions 
    {
        public static void UseSqlsugar(this IApplicationBuilder app)
        {
            app.UseSqlsugarCodeFirstServer();
        }

        public static IServiceCollection AddSqlsugar(this IServiceCollection services)
        {
            services.AddTransient(typeof(IRepository<>), typeof(SqlsugarRepository<>));
            services.Replace(new ServiceDescriptor(typeof(IUnitOfWorkManager), typeof(SqlsugarUnitOfWorkManager), ServiceLifetime.Singleton));
            services.Replace(new ServiceDescriptor(typeof(IDataFilter), typeof(SqlsugarDataFilter), ServiceLifetime.Scoped));
            services.Configure<DbConnOptions>(Appsettings.appConfiguration("DbConnOptions"));

            //使用db上下文
            services.AddDbSqlsugarContextServer();
            return services;
        }
    }
}